using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using T2.WebSecurity.Data.Entities;
using T2.WebSecurity.Data.Entities.Interfaces;
using T2.WebSecurity.Data.Controllers.Interfaces;
using T2.WebSecurity.Data.Entities.Search.Interfaces;
using T2.WebSecurity.Data.Entities.Search;

namespace T2.WebSecurity.Data.Controllers
{
	public partial class OAuthMembershipController : IOAuthMembershipController
	{
	private IOAuthMembership TranslateOAuthMembershipFromDb(DataRow  _translateFrom)
	{
		IOAuthMembership temp = new OAuthMembership();
		temp.Action = T2.WebSecurity.Common.MyEnum.Actions.None.ToString();
		if(_translateFrom["OAuthMembershipID"] != null && _translateFrom["OAuthMembershipID"].ToString().Length > 0)
		{
		temp.OAuthMembershipID = Convert.ToInt32(_translateFrom["OAuthMembershipID"]);
		}
		else
		{
		temp.OAuthMembershipID = Convert.ToInt32("-1");
		}
		if(_translateFrom["Provider"] != null && _translateFrom["Provider"].ToString().Length > 0)
		{
		temp.Provider = Convert.ToString(_translateFrom["Provider"]);
		}
		else
		{
		temp.Provider = String.Empty;
		}
		if(_translateFrom["ProviderUserID"] != null && _translateFrom["ProviderUserID"].ToString().Length > 0)
		{
		temp.ProviderUserID = Convert.ToString(_translateFrom["ProviderUserID"]);
		}
		else
		{
		temp.ProviderUserID = String.Empty;
		}
		temp.TS = (Byte[])_translateFrom["TS"];
		if(_translateFrom["CreatedBy"] != null && _translateFrom["CreatedBy"].ToString().Length > 0)
		{
		temp.CreatedBy = Convert.ToString(_translateFrom["CreatedBy"]);
		}
		else
		{
		temp.CreatedBy = String.Empty;
		}
		if(_translateFrom["CreatedDt"] != null && _translateFrom["CreatedDt"].ToString().Length > 0)
		{
		temp.CreatedDt = Convert.ToDateTime(_translateFrom["CreatedDt"]);
		}
		else
		{
		temp.CreatedDt = Convert.ToDateTime("01/01/1753");
		}
		if(_translateFrom["ModifiedBy"] != null && _translateFrom["ModifiedBy"].ToString().Length > 0)
		{
		temp.ModifiedBy = Convert.ToString(_translateFrom["ModifiedBy"]);
		}
		else
		{
		temp.ModifiedBy = String.Empty;
		}
		if(_translateFrom["ModifiedDt"] != null && _translateFrom["ModifiedDt"].ToString().Length > 0)
		{
		temp.ModifiedDt = Convert.ToDateTime(_translateFrom["ModifiedDt"]);
		}
		else
		{
		temp.ModifiedDt = Convert.ToDateTime("01/01/1753");
		}
		if(_translateFrom["UserId"] != null && _translateFrom["UserId"].ToString().Length > 0)
		{
		temp.UserId = Convert.ToInt32(_translateFrom["UserId"]);
		}
		else
		{
		temp.UserId = Convert.ToInt32("-1");
		}
		return temp;
	}
	private IOAuthMembership TranslateSearchOAuthMembershipFromDb(DataRow  _translateFrom)
	{
		IOAuthMembership temp = new OAuthMembership();
		temp.Action = T2.WebSecurity.Common.MyEnum.Actions.None.ToString();
		if(_translateFrom["Provider"] != null && _translateFrom["Provider"].ToString().Length > 0)
		{
		temp.Provider = Convert.ToString(_translateFrom["Provider"]);
		}
		else
		{
		temp.Provider = String.Empty;
		}
		return temp;
	}
		public IDictionary<string,Int32> OAuthMembership_Add(IOAuthMembership  _oauthmembership)
		{
			IDictionary<string, int> RetDict = new Dictionary<string, int>();
			string connect = ConfigurationManager.ConnectionStrings["WebSecurity"].ToString();
			SqlConnection db = new SqlConnection(connect);
			SqlCommand cmd = new SqlCommand("OAuthMembership_ins", db);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.Connection.Open();
			try
			{
			SqlParameter sp_OAuthMembershipID = DataHelpers.GetParameter("OAuthMembershipID", SqlDbType.Int, ParameterDirection.Output, 4) ;
			sp_OAuthMembershipID.Value = _oauthmembership.OAuthMembershipID;
			cmd.Parameters.Add(sp_OAuthMembershipID);

			SqlParameter sp_Provider = DataHelpers.GetParameter("Provider", SqlDbType.NVarChar, ParameterDirection.Input, 30) ;
			sp_Provider.Value = _oauthmembership.Provider;
			cmd.Parameters.Add(sp_Provider);

			SqlParameter sp_ProviderUserID = DataHelpers.GetParameter("ProviderUserID", SqlDbType.NVarChar, ParameterDirection.Input, 100) ;
			sp_ProviderUserID.Value = _oauthmembership.ProviderUserID;
			cmd.Parameters.Add(sp_ProviderUserID);

			SqlParameter sp_CreatedBy = DataHelpers.GetParameter("CreatedBy", SqlDbType.VarChar, ParameterDirection.Input, 50) ;
			sp_CreatedBy.Value = _oauthmembership.CreatedBy;
			cmd.Parameters.Add(sp_CreatedBy);

			SqlParameter sp_CreatedDt = DataHelpers.GetParameter("CreatedDt", SqlDbType.DateTime, ParameterDirection.Input, 13) ;
			sp_CreatedDt.Value = _oauthmembership.CreatedDt;
			cmd.Parameters.Add(sp_CreatedDt);

			SqlParameter sp_ModifiedBy = DataHelpers.GetParameter("ModifiedBy", SqlDbType.VarChar, ParameterDirection.Input, 50) ;
			sp_ModifiedBy.Value = _oauthmembership.ModifiedBy;
			cmd.Parameters.Add(sp_ModifiedBy);

			SqlParameter sp_ModifiedDt = DataHelpers.GetParameter("ModifiedDt", SqlDbType.DateTime, ParameterDirection.Input, 13) ;
			sp_ModifiedDt.Value = _oauthmembership.ModifiedDt;
			cmd.Parameters.Add(sp_ModifiedDt);

			SqlParameter sp_UserId = DataHelpers.GetParameter("UserId", SqlDbType.Int, ParameterDirection.Input, 4) ;
			sp_UserId.Value = _oauthmembership.UserId;
			cmd.Parameters.Add(sp_UserId);

			SqlParameter sp_RowsAffected = DataHelpers.GetParameter("RowsAffected", SqlDbType.Int, ParameterDirection.Output, 4) ;
			cmd.Parameters.Add(sp_RowsAffected);

			int RetVal = cmd.ExecuteNonQuery();
			KeyValuePair<string, int> KVPRetVal = new KeyValuePair<string, int>("Return_Value", RetVal);
			RetDict.Add(KVPRetVal);
			int Affected = Convert.ToInt32(sp_RowsAffected.Value);
			KeyValuePair<string, int> RARetVal = new KeyValuePair<string, int>("Rows_Affected", Affected);
			RetDict.Add(RARetVal);
			int PkOAuthMembershipID = Convert.ToInt32(sp_OAuthMembershipID.Value);
			KeyValuePair<string, int> PKOAuthMembershipID = new KeyValuePair<string, int>("PKOAuthMembershipID", PkOAuthMembershipID);
			RetDict.Add(PKOAuthMembershipID);
			return RetDict;
			}
			catch(Exception ex)
			{
			throw ex;
			}
			finally
			{
			if (cmd != null)
			{
			cmd.Connection.Close();
			cmd.Dispose();
			}
			}
		}
		public IDictionary<string,Int32> OAuthMembership_Modify(IOAuthMembership  _oauthmembership)
		{
			IDictionary<string, int> RetDict = new Dictionary<string, int>();
			string connect = ConfigurationManager.ConnectionStrings["WebSecurity"].ToString();
			SqlConnection db = new SqlConnection(connect);
			SqlCommand cmd = new SqlCommand("OAuthMembership_udt", db);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.Connection.Open();
			try
			{
			SqlParameter sp_OAuthMembershipID = DataHelpers.GetParameter("OAuthMembershipID", SqlDbType.Int, ParameterDirection.Input, 4) ;
			sp_OAuthMembershipID.Value = _oauthmembership.OAuthMembershipID;
			cmd.Parameters.Add(sp_OAuthMembershipID);

			SqlParameter sp_Provider = DataHelpers.GetParameter("Provider", SqlDbType.NVarChar, ParameterDirection.Input, 30) ;
			sp_Provider.Value = _oauthmembership.Provider;
			cmd.Parameters.Add(sp_Provider);

			SqlParameter sp_ProviderUserID = DataHelpers.GetParameter("ProviderUserID", SqlDbType.NVarChar, ParameterDirection.Input, 100) ;
			sp_ProviderUserID.Value = _oauthmembership.ProviderUserID;
			cmd.Parameters.Add(sp_ProviderUserID);

			SqlParameter sp_TS = DataHelpers.GetParameter("TS", SqlDbType.Timestamp, ParameterDirection.Input, 8) ;
			sp_TS.Value = _oauthmembership.TS;
			cmd.Parameters.Add(sp_TS);

			SqlParameter sp_CreatedBy = DataHelpers.GetParameter("CreatedBy", SqlDbType.VarChar, ParameterDirection.Input, 50) ;
			sp_CreatedBy.Value = _oauthmembership.CreatedBy;
			cmd.Parameters.Add(sp_CreatedBy);

			SqlParameter sp_CreatedDt = DataHelpers.GetParameter("CreatedDt", SqlDbType.DateTime, ParameterDirection.Input, 13) ;
			sp_CreatedDt.Value = _oauthmembership.CreatedDt;
			cmd.Parameters.Add(sp_CreatedDt);

			SqlParameter sp_ModifiedBy = DataHelpers.GetParameter("ModifiedBy", SqlDbType.VarChar, ParameterDirection.Input, 50) ;
			sp_ModifiedBy.Value = _oauthmembership.ModifiedBy;
			cmd.Parameters.Add(sp_ModifiedBy);

			SqlParameter sp_ModifiedDt = DataHelpers.GetParameter("ModifiedDt", SqlDbType.DateTime, ParameterDirection.Input, 13) ;
			sp_ModifiedDt.Value = _oauthmembership.ModifiedDt;
			cmd.Parameters.Add(sp_ModifiedDt);

			SqlParameter sp_UserId = DataHelpers.GetParameter("UserId", SqlDbType.Int, ParameterDirection.Input, 4) ;
			sp_UserId.Value = _oauthmembership.UserId;
			cmd.Parameters.Add(sp_UserId);

			SqlParameter sp_RowsAffected = DataHelpers.GetParameter("RowsAffected", SqlDbType.Int, ParameterDirection.Output, 4) ;
			cmd.Parameters.Add(sp_RowsAffected);
			int RetVal = cmd.ExecuteNonQuery();
			KeyValuePair<string, int> KVPRetVal = new KeyValuePair<string, int>("Return_Value", RetVal);
			RetDict.Add(KVPRetVal);
			int Affected = Convert.ToInt32(sp_RowsAffected.Value);
			KeyValuePair<string, int> RARetVal = new KeyValuePair<string, int>("Rows_Affected", Affected);
			RetDict.Add(RARetVal);
			int PkOAuthMembershipID = Convert.ToInt32(sp_OAuthMembershipID.Value);
			KeyValuePair<string, int> PKOAuthMembershipID = new KeyValuePair<string, int>("PKOAuthMembershipID", PkOAuthMembershipID);
			RetDict.Add(PKOAuthMembershipID);
			return RetDict;
			}
			catch(Exception ex)
			{
			throw ex;
			}
			finally
			{
			if (cmd != null)
			{
			cmd.Connection.Close();
			cmd.Dispose();
			}
			}
		}
		public IDictionary<string, int> OAuthMembership_Remove(IOAuthMembership  _oauthmembership)
		{
			IDictionary<string, int> RetDict = new Dictionary<string, int>();
			string connect = ConfigurationManager.ConnectionStrings["WebSecurity"].ToString();
			SqlConnection db = new SqlConnection(connect);
			SqlCommand cmd = new SqlCommand("OAuthMembership_del", db);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.Connection.Open();
			try
			{
			SqlParameter sp_OAuthMembershipID = DataHelpers.GetParameter("OAuthMembershipID", SqlDbType.Int, ParameterDirection.Input, 4) ;
			sp_OAuthMembershipID.Value = _oauthmembership.OAuthMembershipID;
			cmd.Parameters.Add(sp_OAuthMembershipID);

			SqlParameter sp_TS = DataHelpers.GetParameter("TS", SqlDbType.Timestamp, ParameterDirection.Input, 8) ;
			sp_TS.Value = _oauthmembership.TS;
			cmd.Parameters.Add(sp_TS);

			SqlParameter sp_RowsAffected = DataHelpers.GetParameter("RowsAffected", SqlDbType.Int, ParameterDirection.Output, 4) ;
			cmd.Parameters.Add(sp_RowsAffected);
			int RetVal = cmd.ExecuteNonQuery();
			KeyValuePair<string, int> KVPRetVal = new KeyValuePair<string, int>("Return_Value", RetVal);
			RetDict.Add(KVPRetVal);
			int Affected = Convert.ToInt32(sp_RowsAffected.Value);
			KeyValuePair<string, int> RARetVal = new KeyValuePair<string, int>("Rows_Affected", Affected);
			RetDict.Add(RARetVal);
			return RetDict;
			}
			catch(Exception ex)
			{
			throw ex;
			}
			finally
			{
			if (cmd != null)
			{
			cmd.Connection.Close();
			cmd.Dispose();
			db = null;
			}
			}
		}
		public IOAuthMembership OAuthMembership_GetOne(IOAuthMembership  _oauthmembership)
		{
			string connect = ConfigurationManager.ConnectionStrings["WebSecurity"].ToString();
			SqlConnection db = new SqlConnection(connect);
			SqlCommand cmd = new SqlCommand("OAuthMembership_GetOne", db);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.Connection.Open();
			try
			{
			SqlParameter sp_OAuthMembershipID = DataHelpers.GetParameter("OAuthMembershipID", SqlDbType.Int, ParameterDirection.Input, 4) ;
			sp_OAuthMembershipID.Value = _oauthmembership.OAuthMembershipID;
			cmd.Parameters.Add(sp_OAuthMembershipID);

			SqlParameter sp_RowsAffected = DataHelpers.GetParameter("RowsAffected", SqlDbType.Int, ParameterDirection.Output, 4) ;
			cmd.Parameters.Add(sp_RowsAffected);
			DataSet ds = new DataSet();
			SqlDataAdapter da = new SqlDataAdapter(cmd);
			da.Fill(ds);
			int Affected = Convert.ToInt32(sp_RowsAffected.Value);
			IOAuthMembership RetVal = new OAuthMembership();
			if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
			{
			RetVal = TranslateOAuthMembershipFromDb(ds.Tables[0].Rows[0]);
			RetVal.TotalRows = Affected;
			}
			return RetVal;
			}
			catch(Exception ex)
			{
			throw ex;
			}
			finally
			{
			if (cmd != null)
			{
			cmd.Connection.Close();
			cmd.Dispose();
			db = null;
			}
			}
		}
		public List<IOAuthMembership> OAuthMembership_GetAll(IOAuthMembership  _oauthmembership)
		{
			string connect = ConfigurationManager.ConnectionStrings["WebSecurity"].ToString();
			SqlConnection db = new SqlConnection(connect);
			SqlCommand cmd = new SqlCommand("OAuthMembership_GetAll", db);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.Connection.Open();
			try
			{
			SqlParameter sp_PageSize = DataHelpers.GetParameter("PageSize", SqlDbType.Int, ParameterDirection.Input, 4) ;
			sp_PageSize.Value = _oauthmembership.PageSize;
			cmd.Parameters.Add(sp_PageSize);
			SqlParameter sp_PageNumber = DataHelpers.GetParameter("PageNumber", SqlDbType.Int, ParameterDirection.Input, 4) ;
			sp_PageNumber.Value = _oauthmembership.PageNumber;
			cmd.Parameters.Add(sp_PageNumber);
			SqlParameter sp_SortColumn = DataHelpers.GetParameter("SortColumn", SqlDbType.NVarChar, ParameterDirection.Input, 50) ;
			sp_SortColumn.Value = _oauthmembership.SortColumn;
			cmd.Parameters.Add(sp_SortColumn);
			SqlParameter sp_SortType = DataHelpers.GetParameter("SortType", SqlDbType.NVarChar, ParameterDirection.Input, 10) ;
			sp_SortType.Value = _oauthmembership.SortType;
			cmd.Parameters.Add(sp_SortType);
			SqlParameter sp_TotalRows = DataHelpers.GetParameter("TotalRows", SqlDbType.Int, ParameterDirection.Output, 4) ;
			cmd.Parameters.Add(sp_TotalRows);
			SqlParameter sp_RowsAffected = DataHelpers.GetParameter("RowsAffected", SqlDbType.Int, ParameterDirection.Output, 4) ;
			cmd.Parameters.Add(sp_RowsAffected);
			DataSet ds = new DataSet();
			SqlDataAdapter da = new SqlDataAdapter(cmd);
			da.Fill(ds);
			int Affected = Convert.ToInt32(sp_RowsAffected.Value);
			List<IOAuthMembership> tempList = new List<IOAuthMembership>();
			IOAuthMembership temp = new OAuthMembership();
			foreach(DataTable tb in ds.Tables)
			{
				foreach(DataRow r in tb.Rows)
				{
					temp.TotalRows = Convert.ToInt32(sp_TotalRows.Value);
					temp = TranslateOAuthMembershipFromDb(r);
					tempList.Add(temp);
				}
			}
			return tempList;
			}
			catch(Exception ex)
			{
			throw ex;
			}
			finally
			{
			if (cmd != null)
			{
			cmd.Connection.Close();
			cmd.Dispose();
			db = null;
			}
			}
		}
		public List<IOAuthMembership> OAuthMembership_Search(IOAuthMembershipSearch _oauthmembership)
		{
			string connect = ConfigurationManager.ConnectionStrings["WebSecurity"].ToString();
			SqlConnection db = new SqlConnection(connect);
			SqlCommand cmd = new SqlCommand("OAuthMembership_Search", db);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.Connection.Open();
			try
			{
			SqlParameter sp_Provider = DataHelpers.GetParameter("Provider", SqlDbType.NVarChar, ParameterDirection.Input, 30) ;
			sp_Provider.Value = _oauthmembership.Provider;
			cmd.Parameters.Add(sp_Provider);

			SqlParameter sp_PageSize = DataHelpers.GetParameter("PageSize", SqlDbType.Int, ParameterDirection.Input, 4) ;
			sp_PageSize.Value = _oauthmembership.PageSize;
			cmd.Parameters.Add(sp_PageSize);
			SqlParameter sp_PageNumber = DataHelpers.GetParameter("PageNumber", SqlDbType.Int, ParameterDirection.Input, 4) ;
			sp_PageNumber.Value = _oauthmembership.PageNumber;
			cmd.Parameters.Add(sp_PageNumber);
			SqlParameter sp_SortColumn = DataHelpers.GetParameter("SortColumn", SqlDbType.NVarChar, ParameterDirection.Input, 50) ;
			sp_SortColumn.Value = _oauthmembership.SortColumn;
			cmd.Parameters.Add(sp_SortColumn);
			SqlParameter sp_SortType = DataHelpers.GetParameter("SortType", SqlDbType.NVarChar, ParameterDirection.Input, 10) ;
			sp_SortType.Value = _oauthmembership.SortType;
			cmd.Parameters.Add(sp_SortType);
			SqlParameter sp_TotalRows = DataHelpers.GetParameter("TotalRows", SqlDbType.Int, ParameterDirection.Output, 4) ;
			cmd.Parameters.Add(sp_TotalRows);
			SqlParameter sp_RowsAffected = DataHelpers.GetParameter("RowsAffected", SqlDbType.Int, ParameterDirection.Output, 4) ;
			cmd.Parameters.Add(sp_RowsAffected);
			DataSet ds = new DataSet();
			SqlDataAdapter da = new SqlDataAdapter(cmd);
			da.Fill(ds);
			int Affected = Convert.ToInt32(sp_RowsAffected.Value);
			List<IOAuthMembership> tempList = new List<IOAuthMembership>();
			foreach(DataTable tb in ds.Tables)
			{
				foreach(DataRow r in tb.Rows)
				{
					IOAuthMembership temp = new OAuthMembership();
					temp = TranslateOAuthMembershipFromDb(r);
					tempList.Add(temp);
				}
			}
			return tempList;
			}
			catch(Exception ex)
			{
			throw ex;
			}
			finally
			{
			if (cmd != null)
			{
			cmd.Connection.Close();
			cmd.Dispose();
			db = null;
			}
			}
		}
		public List<IOAuthMembership> UserProfile_OAuthMembership_ByParent(IOAuthMembership _oauthmembership)
		{
			string connect = ConfigurationManager.ConnectionStrings["WebSecurity"].ToString();
			SqlConnection db = new SqlConnection(connect);
			SqlCommand cmd = new SqlCommand("UserProfile_dbo_OAuthMembership_ByParent", db);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.Connection.Open();
			try
			{
			SqlParameter sp_UserId = DataHelpers.GetParameter("UserId", SqlDbType.Int, ParameterDirection.Input, 4) ;
			sp_UserId.Value = _oauthmembership.UserId;
			cmd.Parameters.Add(sp_UserId);

			SqlParameter sp_PageSize = DataHelpers.GetParameter("PageSize", SqlDbType.Int, ParameterDirection.Input, 4) ;
			sp_PageSize.Value = _oauthmembership.PageSize;
			cmd.Parameters.Add(sp_PageSize);
			SqlParameter sp_PageNumber = DataHelpers.GetParameter("PageNumber", SqlDbType.Int, ParameterDirection.Input, 4) ;
			sp_PageNumber.Value = _oauthmembership.PageNumber;
			cmd.Parameters.Add(sp_PageNumber);
			SqlParameter sp_SortColumn = DataHelpers.GetParameter("SortColumn", SqlDbType.NVarChar, ParameterDirection.Input, 50) ;
			sp_SortColumn.Value = _oauthmembership.SortColumn;
			cmd.Parameters.Add(sp_SortColumn);
			SqlParameter sp_SortType = DataHelpers.GetParameter("SortType", SqlDbType.NVarChar, ParameterDirection.Input, 10) ;
			sp_SortType.Value = _oauthmembership.SortType;
			cmd.Parameters.Add(sp_SortType);
			SqlParameter sp_TotalRows = DataHelpers.GetParameter("TotalRows", SqlDbType.Int, ParameterDirection.Output, 4) ;
			cmd.Parameters.Add(sp_TotalRows);
			SqlParameter sp_RowsAffected = DataHelpers.GetParameter("RowsAffected", SqlDbType.Int, ParameterDirection.Output, 4) ;
			cmd.Parameters.Add(sp_RowsAffected);
			//int Affected = Convert.ToInt32(sp_RowsAfected.Value);
			DataSet ds = new DataSet();
			SqlDataAdapter da = new SqlDataAdapter(cmd);
			da.Fill(ds);
			int Affected = Convert.ToInt32(sp_RowsAffected.Value);
			List<IOAuthMembership> tempList = new List<IOAuthMembership>();
			foreach(DataTable tb in ds.Tables)
			{
				foreach(DataRow r in tb.Rows)
				{
					IOAuthMembership temp = new OAuthMembership();
					temp.TotalRows = Convert.ToInt32(sp_TotalRows.Value);
					temp = TranslateOAuthMembershipFromDb(r);
					tempList.Add(temp);
				}
			}
			return tempList;
			}
			catch(Exception ex)
			{
			throw ex;
			}
			finally
			{
			if (cmd != null)
			{
			cmd.Connection.Close();
			cmd.Dispose();
			db = null;
			}
			}
		}

    }
}
